package com.cc.timer.spring;

import java.time.LocalDateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
@EnableAsync
@EnableScheduling
public class SpringTaskDemo {
    private static final Logger log = LoggerFactory.getLogger(SpringTaskDemo.class);
//    @Async 代表该任务可以进行异步工作，由原本的串行改为并行
    @Async
    @Scheduled(cron = "0/3 * * * * *")
    public void scheduled1() throws InterruptedException {
//        Thread.sleep(3000);
        log.info("scheduled1 每3秒执行一次：{}", LocalDateTime.now());
    }

    @Scheduled(fixedRate = 4000)
    public void scheduled2() throws InterruptedException {
        Thread.sleep(1000);
        log.info("scheduled2 每4秒执行一次：{}", LocalDateTime.now());
    }

    @Scheduled(fixedDelay = 5000)
    public void scheduled3() throws InterruptedException {
        Thread.sleep(1000);
        log.info("scheduled3 上次执行完毕后隔5秒继续执行：{}", LocalDateTime.now());
    }

}